package com.rc.evcharger.redis;

import com.rc.evcharger.common.base.BaseRedis;
import com.rc.evcharger.common.redis.RedisDao;
import com.rc.evcharger.common.utils.DataUtil;
import com.rc.evcharger.common.utils.TimeoutUtil;
import io.netty.util.Timeout;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Redis存取工具
 */
@Service
public class RedisServiceImpl implements RedisService {

    @Autowired
    private RedisDao redisDao;

    @Override
    public void put(String key, Object value) {
        redisDao.putSession(key, value);
    }

    @Override
    public Object get(String key) {
        return redisDao.getSession(key);
    }

    @Override
    public void remove(String key) {
        redisDao.deleteSession(key);
    }

    @Override
    public void putExOrder() {
        List<String> list = new ArrayList<>();
        TimeoutUtil timeoutUtil = TimeoutUtil.newInstance();
        Map<String, Timeout> timeoutMap = timeoutUtil.getTimeoutMap();
        for (Map.Entry<String, Timeout> entry : timeoutMap.entrySet()) {
            list.add(entry.getKey());
        }
        redisDao.putSession("timeout", list);
    }

    @Override
    public List<String> getExOrder() {
        return (List<String>) redisDao.getSession("timeout");
    }

    @Override
    public void deleteCurrentData(String rtua, String gunNum, String orderNum) {
        String[] keys = {DataUtil.dataCodes[3][0].toString(), DataUtil.dataCodes[5][0].toString(),
                DataUtil.dataCodes[7][0].toString(), DataUtil.dataCodes[8][0].toString(),
                DataUtil.dataCodes[9][0].toString(), DataUtil.dataCodes[10][0].toString(),
                DataUtil.dataCodes[12][0].toString(), DataUtil.dataCodes[13][0].toString(),
                DataUtil.dataCodes[14][0].toString(), DataUtil.dataCodes[21][0].toString()};
        for (String key : keys) {
            key = DataUtil.getCurrDataKey(rtua, gunNum, key, orderNum);
            redisDao.deleteSession(key);
        }

    }

    @Override
    public void putListToSession(String key, List<String> valueList) {
        redisDao.putListSession(key, valueList);
    }

    @Override
    public void putMapToSession(String key, Map<String, String> map) {
        redisDao.putHashAllSession(key, map);

    }

    @Override
    public List<String> findByKeyLike(String key) {
        return (List<String>) redisDao.keys(key);
    }

    /*
     * @Override public Map<String, Long> getIds(BaseRedis br) { List<String>
     * keys=(List<String>) redisDao.keys(br.getKey()+"*"); Map<String,Long>
     * result=new HashMap<>(); for(String key:keys){ result.put(key,
     * (Long)redisDao.getSession(key)); } return result; }
     */
    @Override
    public List<? extends BaseRedis> getObjects(BaseRedis br) {
        List<String> keys = (List<String>) redisDao.keys(br.getKey() + "*");
        List<BaseRedis> result = new ArrayList<>();
        for (String key : keys) {
            String redisValue = redisDao.getRedis(key);
            if (redisValue == null || redisValue.equals("") || redisValue.equals("null")) {
                continue;
            }
            if (br instanceof RedOrder) {
                //result.add(new Order(key, (Long)redisDao.getSession(key)));
                result.add(new RedOrder(key, redisValue));
            } else {
                //result.add(new Member(key, (Long)redisDao.getSession(key)));
                result.add(new RedMember(key, Long.valueOf(redisValue)));
            }
        }
        return result;
    }

    @Override
    public void addToRedis(BaseRedis br) {
        //redisDao.putSession(br.getKey(), br.getValue());
        redisDao.putRedis(br.getKey(), String.valueOf(br.getValue()));
    }

    @Override
    public String getFromRedis(BaseRedis br) {
        //return redisDao.getSession(br.getKey());
        return redisDao.getRedis(br.getKey());
    }

    @Override
    public void putRedis(String key, String value) {
        redisDao.putRedis(key, value);

    }

    @Override
    public String getRedis(String key) {
        return redisDao.getRedis(key);
    }

    @Override
    public void leftPush(String key, RedPile pile) {
        pile = new RedPile("88 88 88 88", "01", new BigDecimal(1), new BigDecimal(1), new BigDecimal(1));
        System.out.println(pile.toString());
        redisDao.leftPushList("list", pile.toString());

    }

    @Override
    public RedPile getPile(String key) {
        return new RedPile((String) redisDao.rightPopList("list"));
    }


    @Override
    public Long getListSize(String key) {

        return redisDao.listSessionSize(key);
    }

    @Override
    public boolean addExpire(String key, Object value, Integer milliseconds) {
        Object redis = redisDao.getOpsForRedis(key);
        if (redis == null) {
            //redisDao.putRedis(key, "1");
            redisDao.addExpireValue(key, 1, milliseconds, TimeUnit.MILLISECONDS);
            return false;
        }
        return true;
    }

    @Override
    public boolean setIfAbsent(String key, Object value, long time) {
        return redisDao.setIfAbsent(key, value, time);
    }

}
